﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Olympic_email_to_future.Module;
using Olympic_email_to_future.BLLFactory;
using Olympic_email_to_future.IBLL;
using Olympic_email_to_future.DALFactory;
using Olympic_email_to_future.IDAL;
using Olympic_email_to_future.DAL;
using System.Data.Linq;

namespace FutureMeWeb
{
    public partial class write : System.Web.UI.Page, ICallbackEventHandler
    {
        IMailBLL bll = BusinessFactory.CreateEmailBLL();
        IAttachmentBLL bllAtt = BusinessFactory.CreateAttachmentBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                UserInfo user = (UserInfo)Session["User"];
                AccountBtn.Text = user.userMailAddress;
                LoginBtn.Visible = false;
                RegisterBtn.Visible = false;
            }
            else
            {
                AccountBtn.Visible = false;
                LogoffBtn.Visible = false;
            };
            if (!IsPostBack)
            {
                setList();
            }

        }
        public string str;
        /// <summary>
        /// 实现ICallbackEventHandler 接口，返回处理结果
        /// </summary>
        /// <returns></returns>
        public string GetCallbackResult()
        {
            return str;
        }
        public void RaiseCallbackEvent(string eventArgument)
        {
            if (string.IsNullOrEmpty(eventArgument))
            {
                str = "false";
            }
            else
            {
                str = "true";
            }
        }

        public void btnSend_Click(Object o,EventArgs e)
        {
            //Response.Write("发送按钮");
            //检查顺序----邮箱》title不为空》content不为空》时间问题》验证码》

            int year = Convert.ToInt32(yearList.SelectedValue.ToString());
            int month = Convert.ToInt32(monthList.SelectedValue.ToString());
            int day = Convert.ToInt32(dayList.SelectedValue.ToString());
            int hour = Convert.ToInt32(hourList.SelectedValue.ToString());
            int min = Convert.ToInt32(minList.SelectedValue.ToString());
            string subject = txb_email_title.Text;
            string mailAddress = txb_email_address.Text;
            string content = txb_email_content.Text;
            string validate = txbValidate.Text;

            if (mailAddress.Length == 0)
            {
                ;
            }
            else if (subject.Length == 0)
            {
                ;
            }
            else if (content.Length == 0)
            {
                ;
            }



            if (!(txbValidate.Text.ToLower().Equals(Session["validate_code"].ToString().ToLower())))
            {

                txbValidate.Text = "";
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('验证码错误')", true);
            }
            else
            {

                MailInfo mailToInsert = new MailInfo();
                Response.Write("验证成功！");
                mailToInsert.mailState = 0;//发送状态初始化为0
                mailToInsert.mailSenderAddress = "futureme.com";
                mailToInsert.mailReceiverAddress = txb_email_address.Text.ToString();
                mailToInsert.mailCreateDate = DateTime.Now;
                mailToInsert.mailSendDate = new DateTime(year, month, day, hour, min, 0);
                mailToInsert.mailTitle = txb_email_title.Text.ToString();
                mailToInsert.mailContent = txb_email_content.Text.ToString();
                if (rbl_email_state.SelectedValue == "private")
                {
                    mailToInsert.mailIsPublic = 0;//private
                }
                else
                {
                    mailToInsert.mailIsPublic = 1;
                }

                //返回insert那条数据的id值
                int emailid = bll.doinsertMailInfo(mailToInsert);
                //将附件插入表中
                attachmentHandle(emailid);



                Response.Write("<script>alert('发送成功!');location.href='successWrite.aspx';</script>");
            }

        }

        public void attachmentHandle(int emailId)  //将附件插入表中 
        {
            for (int i=0;i<Request.Files.Count ;i++ )
            {
                Stream streamAtt = Request.Files[i].InputStream;
                String name= Request.Files[i].FileName;
                int attLength = Convert.ToInt32(streamAtt.Length.ToString());
                byte[] fileByteArray=new byte[attLength];
                streamAtt.Read(fileByteArray, 0, attLength);
                AttachmentInfo att = new AttachmentInfo();
                att.emailId = emailId;
                att.attachmentName = name;
                att.attachmentLength = Convert.ToInt32(streamAtt.Length.ToString());
                att.attachmentEntity = fileByteArray;
                bllAtt.doinsertAttmentInfo(att);

            }


        }
        public void setList()
        {
            for (int i = 0; i < 24; i++)
            {
               hourList.Items.Add(new ListItem(i.ToString()));
              
                
            }
            for (int i = 0; i < 60; i++)
            {
               minList.Items.Add(new ListItem(i.ToString()));
              

            }
            for (int i = 2013; i < 2030; i++)
            {
                yearList.Items.Add(new ListItem(i.ToString()));

            }
            for (int i = 1; i < 13; i++)
            {
                monthList.Items.Add(new ListItem(i.ToString()));

            }
            for (int i = 1; i < 32; i++)
            {
                dayList.Items.Add(new ListItem(i.ToString()));

            }


        }


    }
}